﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using ueEngine.Biz.Core.AppService.Recruitment;
using ueEngine.Biz.Inf.LogicEntity;
using ueEngine.Biz.Core.ExServer.WorkPool;
using ueEngine.Biz.Core.ExServer.WorkPool.WorkHandle.Email;

namespace ueEngine.Web.Controllers
{
    [Authorize]
    public class ManageController : Controller
    {
        //
        // GET: /Manage/

        public ActionResult Index()
        {
            if (HttpContext.User.Identity.Name.ToLower() !=
                "admin@yaojianzhi.com")
            {
                return Redirect("/");
            }
            var instance = new RecruitService(HttpContext.User.Identity.Name);
            var data = instance.QueryRecruitment(0, 20,
                 Biz.Inf.LogicEntity.RecruitSheetType.NoDefine, true, true);
            ViewData["data"] = data;
            return View();
        }
        public ActionResult pass(int id,int positionId, int recommand=0)
        {
            var model = new RecruitSheetModel().Element(c=>c.Id == id);
            if (model != null)
            {
                if (!model.IsVerify)
                {
                    model.IsVerify = true;
                    model.PostModify();
                }
            }
            if (recommand == 1)
            {
                var position = new PositionModel().Element(c => c.Id == positionId);
                if (position != null)
                {
                    position.PositionLevel = PositionLevel.Special;
                    position.PostModify();

                    var recuritment = new RecruitSheetModel().Element(c => c.Id == position.RecruitId);
                    if (recuritment != null)
                    {
                        recuritment.RecruitSubject = RecruitSheetSubject.Recommand;
                        recuritment.PostModify();
                    }
                }
            }

            WorkPool.Append<AutoPushMessage>(WorkType.email_for_recruitmentexamined,
                new AutoPushMessage()
                {
                    Param = new string[2] { model.Caption, model.Id.ToString() },
                    Recipient = model.Recruiter
                }, null);
            return RedirectToAction("Index");
        }
        public ActionResult delete(int id, int positionId)
        {
            var recuritSheet = new RecruitSheetModel().Element(c => c.Id == id);
            var positions = new PositionModel().Elements(c => c.RecruitId == id);
            if (recuritSheet != null && positions != null && positions.Any() &&
                    positions.Any(c => c.Id == positionId))
            {
                positions.FirstOrDefault(c => c.Id == positionId).PostDelete();
                if (positions.Count() == 1)
                {
                    var depositCompany = new DepositCompanyModel().Element(c => c.Id == recuritSheet.DepositCompanyId);
                    if (depositCompany != null)
                    {
                        depositCompany.PostDelete();
                    }
                    recuritSheet.PostDelete();
                }
            }
            return RedirectToAction("Index");
        }
    }
}
